home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 74 / IOPROG_74.ISO / soft / Codice / Libro Visual Basic e Database / Documento.vb < prev    next >
Encoding:
Text File  |  2003-09-30  |  6.5 KB  |  193 lines

  1. Imports System.Data.SqlClient
  2. Public Class Documento
  3.     Public Key As String
  4.     Public ondb As Boolean
  5.     Private mIdDocumento As Integer
  6.     Private mIntestatario As Integer
  7.     Private mData As Date
  8.     Private mTipo As String
  9.     Private mTotaleDocumento As Decimal
  10.     Private mColRigaDocumento As ColRigaDocumento
  11.     Private mDescrizioneIntestatario As String
  12.  
  13.     Public Property DescrizioneIntestatario() As String
  14.         Get
  15.             DescrizioneIntestatario = mDescrizioneIntestatario
  16.         End Get
  17.         Set(ByVal Value As String)
  18.             mDescrizioneIntestatario = Value
  19.         End Set
  20.     End Property
  21.  
  22.     Public Property ColRigaDocumento() As ColRigaDocumento
  23.         Get
  24.             If mColRigaDocumento Is Nothing Then
  25.                 mColRigaDocumento = New ColRigaDocumento()
  26.             End If
  27.             ColRigaDocumento = mColRigaDocumento
  28.         End Get
  29.         Set(ByVal Value As ColRigaDocumento)
  30.             mColRigaDocumento = Value
  31.         End Set
  32.     End Property
  33.     Public Sub RetrivelistRigaDocumento()
  34.         Dim objRigaDocumento As RigaDocumento
  35.         Dim QuerySql As String
  36.         QuerySql = "select * from RigaDocumento where IdDocumento=" & IdDocumento
  37.         Dim ObjCommand As New SqlCommand(QuerySql, ObjConnection)
  38.         Dim ObjReader As SqlDataReader = ObjCommand.ExecuteReader()
  39.         mColRigaDocumento = New ColRigaDocumento()
  40.         Try
  41.             While ObjReader.Read()
  42.                 objRigaDocumento = New RigaDocumento()
  43.                 With objRigaDocumento
  44.                     .IdRiga = ObjReader.GetInt32(0)
  45.                     .Key = .IdRiga
  46.                     .IdDocumento = ObjReader.GetInt32(1)
  47.                     .IdArticolo = ObjReader.GetInt32(2)
  48.                     .Quantita = ObjReader.GetValue(3)
  49.                     .PrezzoCosto = ObjReader.GetDecimal(4)
  50.                     .Iva = ObjReader.GetValue(5)
  51.                     .ondb = True
  52.                 End With
  53.                 mColRigaDocumento.Add(objRigaDocumento)
  54.             End While
  55.         Finally
  56.             ObjReader.Close()
  57.         End Try
  58.     End Sub
  59.  
  60.  
  61.     Public Property IdDocumento() As Integer
  62.         Get
  63.             IdDocumento = mIdDocumento
  64.         End Get
  65.         Set(ByVal Value As Integer)
  66.             mIdDocumento = Value
  67.         End Set
  68.     End Property
  69.     Public Property Intestatario() As Integer
  70.         Get
  71.             Intestatario = mIntestatario
  72.         End Get
  73.         Set(ByVal Value As Integer)
  74.             mIntestatario = Value
  75.         End Set
  76.     End Property
  77.     Public Property Data() As Date
  78.         Get
  79.             Data = mData
  80.         End Get
  81.         Set(ByVal Value As Date)
  82.             mData = Value
  83.         End Set
  84.     End Property
  85.     Public Property Tipo() As String
  86.         Get
  87.             Tipo = mTipo
  88.         End Get
  89.         Set(ByVal Value As String)
  90.             mTipo = Value
  91.         End Set
  92.     End Property
  93.     Public Property TotaleDocumento() As Decimal
  94.         Get
  95.             TotaleDocumento = mTotaleDocumento
  96.         End Get
  97.         Set(ByVal Value As Decimal)
  98.             mTotaleDocumento = Value
  99.         End Set
  100.     End Property
  101.     Public Sub Salva()
  102.         Dim sp As SqlCommand
  103.         Dim spPar As SqlParameter
  104.         Dim ObjReader As SqlDataReader
  105.         Dim strSql As String
  106.         If ondb = True Then
  107.             strSql = "up_update_Documento"
  108.         Else
  109.             strSql = "up_insert_Documento"
  110.         End If
  111.         sp = New SqlCommand(strSql, ObjConnection)
  112.         sp.CommandType = CommandType.StoredProcedure
  113.         spPar = sp.Parameters.Add("@IdDocumento", SqlDbType.Int, 4)
  114.         spPar.Value() = IdDocumento
  115.         If ondb = False Then spPar.Direction = ParameterDirection.InputOutput
  116.         spPar = sp.Parameters.Add("@Intestatario", SqlDbType.Int, 4)
  117.         spPar.Value() = Intestatario
  118.         spPar = sp.Parameters.Add("@Data", SqlDbType.DateTime, 16)
  119.         spPar.Value() = Data
  120.         spPar = sp.Parameters.Add("@Tipo", SqlDbType.Char, 1)
  121.         spPar.Value() = Tipo
  122.         spPar = sp.Parameters.Add("@TotaleDocumento", SqlDbType.Money, 8)
  123.         spPar.Value() = TotaleDocumento
  124.         Try
  125.             ObjReader = sp.ExecuteReader()
  126.         Catch e As Exception
  127.             MsgBox(e.Message)
  128.         Finally
  129.             ObjReader.Close()
  130.         End Try
  131.         If ondb = False Then IdDocumento = sp.Parameters.Item("@IdDocumento").Value
  132.         CancellaRighe()
  133.         Dim tmpriga As RigaDocumento
  134.         For Each tmpriga In ColRigaDocumento.Elements
  135.             tmpriga.IdDocumento = Me.IdDocumento
  136.             tmpriga.Salva()
  137.         Next
  138.  
  139.         ondb = True
  140.     End Sub
  141.     Private Sub CancellaRighe()
  142.         Dim tmpriga As New RigaDocumento()
  143.         tmpriga.IdDocumento = Me.IdDocumento
  144.         tmpriga.Cancella()
  145.         tmpriga = Nothing
  146.     End Sub
  147.     Public Sub Cancella()
  148.         If ondb = False Then Exit Sub
  149.         Dim sp As SqlCommand
  150.         Dim spPar As SqlParameter
  151.         Dim ObjReader As SqlDataReader
  152.         Dim strSql As String
  153.         strSql = "up_delete_Documento"
  154.         sp = New SqlCommand(strSql, ObjConnection)
  155.         sp.CommandType = CommandType.StoredProcedure
  156.         spPar = sp.Parameters.Add("@IdDocumento", SqlDbType.Int, 4)
  157.         spPar.Value() = IdDocumento
  158.         '        spPar = sp.Parameters.Add("@Tipo", SqlDbType.Char, 1)
  159.         '        spPar.Value() = Tipo
  160.         Try
  161.             ObjReader = sp.ExecuteReader()
  162.         Catch e As Exception
  163.             MsgBox(e.Message)
  164.         Finally
  165.             ObjReader.Close()
  166.         End Try
  167.     End Sub
  168.     Public Sub Ricerca(ByVal StringaRicerca As String)
  169.         Dim QuerySql As String
  170.         QuerySql = "select * from Documento"
  171.         If StringaRicerca <> "" Then
  172.             StringaRicerca = " where " + Right(StringaRicerca, Len(StringaRicerca) - 4)
  173.         End If
  174.         QuerySql = QuerySql + StringaRicerca
  175.         Dim ObjCommand As New SqlCommand(QuerySql, ObjConnection)
  176.         Dim ObjReader As SqlDataReader = ObjCommand.ExecuteReader()
  177.         Try
  178.             While ObjReader.Read()
  179.                 IdDocumento = ObjReader.GetInt32(0)
  180.                 Intestatario = ObjReader.GetInt32(1)
  181.                 Data = ObjReader.GetDateTime(2)
  182.                 Tipo = ObjReader.GetString(3)
  183.                 TotaleDocumento = ObjReader.GetDecimal(4)
  184.                 ondb = True
  185.             End While
  186.         Finally
  187.             ObjReader.Close()
  188.         End Try
  189.     End Sub
  190.  
  191.  
  192. End Class
  193.